package br.com.fogas.impressora.business;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import br.com.fogas.database.entidade.NotaFiscal;
import br.com.fogas.impressora.ImpressaoBuilder;
import br.com.fogas.impressora.ImpressoraMPTException;
import br.com.fogas.impressora.ImpressoraMPT_Builder.ALIGN;
import br.com.fogas.impressora.R;
import br.com.fogas.modelo.Configuracao;
import br.com.fogas.utils.dialog.DialogAlert;
import br.com.fogas.utils.dialog.DialogAlertConfirm;
import br.com.fogas.utils.dialog.DialogAlertConfirmListener;
import br.com.fogas.utils.dialog.ProgressDialogBuilder;
import br.com.fogas.utils.task.ServiceTask;
import br.com.fogas.utils.task.ServiceTaskBusiness;

public class Imprimir_1_2_ViaNota_Boleto implements ServiceTaskBusiness {

	private NotaFiscal notaFiscal;
	private Configuracao configuracao;
	private Fragment fragment;
	private ImpressaoBuilder impressaoBuilder;
	
	public Imprimir_1_2_ViaNota_Boleto(Fragment fragment, NotaFiscal notaFiscal, Configuracao configuracao) {
		this.fragment = fragment;
		this.notaFiscal = notaFiscal;
		this.configuracao = configuracao;
	} 
	
	@Override
	public void start(ServiceTask serviceTask) {
		try {
			serviceTask.getDialog().updateDialog("Impressora", "conectando...");
			Thread.sleep(500);
			this.impressaoBuilder = new ImpressaoBuilder();
			this.impressaoBuilder.getImpressoraBuilder().conectar();
			
			serviceTask.getDialog().updateDialog("Nota Fiscal 1 Via", "imprimindo...");
			Thread.sleep(500);
			impressaoBuilder.imprimirNota(notaFiscal, configuracao, 1);
			
			serviceTask.getDialog().updateDialog("Boleto", "imprimindo...");
			Thread.sleep(500);
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinha("------------------------------------------");
			impressaoBuilder.getImpressoraBuilder().imprimirLinha("BOLETO BANCARIO", ALIGN.AL_CENTER);
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.imprimirBoleto(notaFiscal, configuracao);
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			
			if(notaFiscal.isCs_contigencia()) {
				DialogAlert.newInstance()
				.setTitle(R.string.dialog_aviso)
				.setMessage(R.string.dialog_aviso_segunda_via_confirmar)
				.setPositiveClick(via2NotaContigencia)
				.showDialog(fragment.getActivity().getSupportFragmentManager());
			} else {
				DialogAlertConfirm.newInstance(via2NotaOnline)
				.setTitle(R.string.dialog_aviso)
				.setMessage(R.string.dialog_aviso_segunda_via)
				.showDialog(fragment.getActivity().getSupportFragmentManager());
			}
		} catch (Exception e) {
			DialogAlert.newInstance()
			.setTitle(R.string.dialog_title_impressora)
			.setMessage(R.string.dialog_impressora_ligar)
			.showDialog(fragment.getActivity().getSupportFragmentManager());
		}
	}
	
	OnClickListener via2NotaContigencia = new OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			ProgressDialogBuilder progress = new ProgressDialogBuilder(fragment.getActivity())
			.setTitle("Title")
			.setMessage("Message");

			ServiceTask serviceTask = new ServiceTask(progress, new ImprimirSegundaVia());
			serviceTask.execute();
		}
	};
	
	DialogAlertConfirmListener via2NotaOnline = new DialogAlertConfirmListener() {
		
		@Override
		public void onPositiveClick(DialogFragment dialog) {
			ProgressDialogBuilder progress = new ProgressDialogBuilder(fragment.getActivity())
			.setTitle("Title")
			.setMessage("Message");

			ServiceTask serviceTask = new ServiceTask(progress, new ImprimirSegundaVia());
			serviceTask.execute();
		}
		
		@Override
		public void onNegativeClick(DialogFragment dialog) {
			try {
				impressaoBuilder.getImpressoraBuilder().fecharConexao();
			} catch (ImpressoraMPTException e) {
				e.printStackTrace();
			}
		}
	};
	
	private class ImprimirSegundaVia implements ServiceTaskBusiness {

		@Override
		public void start(ServiceTask serviceTask) {
			imprimir2Via(serviceTask);
		}	
	}
	
	private void imprimir2Via(ServiceTask serviceTask) {
		try {
			serviceTask.getDialog().updateDialog("Nota Fiscal 2 Via", "imprimindo...");
			Thread.sleep(500);
			impressaoBuilder.imprimirNota(notaFiscal, configuracao, 2);
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().imprimirLinhaBranco();
			impressaoBuilder.getImpressoraBuilder().fecharConexao();
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ImpressoraMPTException e) {
			e.printStackTrace();
		}
	}
}